package com.hs.mobile.controller;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.GET;

import com.hs.mobile.model.Icon;
import com.hs.mobile.service.icon.IconService;

@Controller
public class IconController {
	private final String IMAGEID = "id";
	private final String WIDTH_IMAGE = "w";
	private final String HEIGHT_IMAGE = "h";
	
	protected final Logger logger = Logger.getLogger(IconController.class);
	@Resource(name="iconsService")
	private IconService iconsServer;
	
	@Autowired
	public IconController( IconService iconsServer) {
		this.iconsServer = iconsServer;
	}
	
	private BufferedImage resizeImage(BufferedImage originalImage, int type, int width, int height ){
		BufferedImage resizedImage = new BufferedImage(width, height, type);
		logger.debug("__ nguyen nam __" + width + ":::"+ height);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(originalImage, 0, 0, width, height, null);
		g.dispose();	 
		return resizedImage;
	}
	
	@GET
	@RequestMapping(value="/icon/{id}", method = RequestMethod.GET)
	@Produces(MediaType.APPLICATION_JSON)
	public @ResponseBody Icon getIcon(@PathVariable("id") Long id )
	{
		logger.debug("getIcon - Json ");
		return iconsServer.findIconById(id);
	}
	
	@GET
	@RequestMapping(value="/iconcategory", method = RequestMethod.GET)
	@Produces("image/*")
	public void getIcon( HttpServletRequest request, HttpServletResponse response ) throws IOException
	{
		logger.debug("getIcon - Image / return Icon ");
		String imageId = request.getParameter(IMAGEID);
		logger.debug("getIcon - Image / return Icon " + imageId);
		String width = request.getParameter(WIDTH_IMAGE);
		String height = request.getParameter(HEIGHT_IMAGE);
		long id = Long.parseLong(imageId);
		int w = 0, h = 0;
		boolean original = true;
		if(width != null && height != null ) { 
		  w = Integer.parseInt(width);
		  h = Integer.parseInt(height);
		  original = false;
		}
		Icon icon = iconsServer.findIconById(id);
		String url = icon.getLinkImage();
		String type = icon.getTypeImage();
		BufferedImage image = ImageIO.read( new URL( url ));
		int typeImage = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
		logger.debug("typeImage = " + typeImage);
        
            // resize the image to fit the GUI's image frame
			if( original == false ) {
				image = resizeImage((BufferedImage) image, typeImage, w, h);
			}
        	final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                ImageIO.write((BufferedImage) image, type, response.getOutputStream());

            //    final byte[] imgData = baos.toByteArray();

            //    final InputStream bigInputStream = 
            //          new ByteArrayInputStream(imgData);
            //    logger.debug("typeImage = " + typeImage);
            //    return Response.ok(bigInputStream).build();
            }
            catch (final IOException e) {
          //      return Response.noContent().build();
            }
        
		//return null;		
	}
}
